﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ANGF
{
	public partial class PreTitle : UserControl
	{
		public PreTitle()
		{
			InitializeComponent();
            シャッフル();
		}

		public MethodInvoker externalCompletion = delegate() { };

		public void SetCompletion(MethodInvoker invoker)
		{
			externalCompletion = invoker;
		}

		public void Completion()
		{
			this.timerAnimation.Stop();
			this.timerCloser.Stop();
			externalCompletion();
			// 2回以上は呼ばない
			externalCompletion = delegate() { };
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			Completion();
		}

		private void buttonCancel_KeyDown(object sender, KeyEventArgs e)
		{
			Completion();
		}

		private void timerCloser_Tick(object sender, EventArgs e)
		{
			Completion();
		}

		private bool isMessageCreating = true;
		private int xGridSize = 4;
		private int yGridSize = 15;
		private int progressCounter = 0;

        private string[] keywords;

		// キーワードをランダムにシャッフル
		private void シャッフル()
		{
            keywords = new string[64];
			for (int i = 0; i < keywords.Length; i++)
			{
                switch (ANGFLib.General.Rand.Next(32))
                {
                    case 0:
                        keywords[i] = "bANGF";
                        break;
                    case 1:
                        keywords[i] = "wANGF";
                        break;
                    default:
                        keywords[i] = "ANGF";
                        break;
                }
				int j = ANGFLib.General.Rand.Next(keywords.Length);
				string t = keywords[i];
				keywords[i] = keywords[j];
				keywords[j] = t;
			}
		}

		private void timerAnimation_Tick(object sender, EventArgs e)
		{
			if (progressCounter >= xGridSize * yGridSize)
			{
				if (isMessageCreating)
				{
					// 作成モード完了なら消去モードに入る
					isMessageCreating = false;
					progressCounter = 0;
					this.BackgroundImage = ANGF.Properties.Resources.mainvis001;
					return;
				}
				// 消去モード完了ならタイマーを止める
				timerAnimation.Enabled = false;
				timerCloser.Enabled = true;
				return;
			}
			if ((progressCounter & 1) == 1)
			{
				addLabel(progressCounter / 2);
			}
			else
			{
				addLabel(xGridSize * yGridSize - progressCounter / 2 - 1);
			}
			progressCounter++;
		}

		private Control[] myControls;
		private static int[] horizontalOrderTable = { 0, 3, 1, 2 };
		private void addLabel(int order)
		{
			if (isMessageCreating)
			{
				Label l = new Label();
				l.Text = keywords[order % keywords.Length];
				l.BackColor = Color.FromArgb(0, 0, 64);
					;
				l.ForeColor = Color.White;
				if (l.Text.StartsWith("b"))
				{
					l.Text = l.Text.Substring(1);
					l.ForeColor = Color.Blue;
				}
				else if (l.Text.StartsWith("r"))
				{
					l.Text = l.Text.Substring(1);
					l.ForeColor = Color.Red;
				}
				else if (l.Text.StartsWith("y"))
				{
					l.Text = l.Text.Substring(1);
					l.ForeColor = Color.Yellow;
				}
				else if (l.Text.StartsWith("w"))
				{
					l.Text = l.Text.Substring(1);
					l.BackColor = Color.White;
					l.ForeColor = Color.Black;
				}

				l.TextAlign = ContentAlignment.MiddleCenter;
				l.Font = new Font(FontFamily.GenericSansSerif, this.Height / yGridSize - 4, GraphicsUnit.Pixel);
				l.Width = this.Width / xGridSize;
				l.Height = this.Height / yGridSize;
				l.Left = l.Width * horizontalOrderTable[order % xGridSize];
				l.Top = l.Height * (order / xGridSize);
				l.Click += (object sender0, EventArgs e0) => { Completion(); };
				this.Controls.Add(l);
				myControls[order] = l;
			}
			else
			{
				this.Controls.Remove(myControls[order]);
			}
		}

        private void PreTitle_Load(object sender, EventArgs e)
		{
			// キャンセルボタンは見えない場所に飛ばす
			this.buttonCancel.Left = -this.buttonCancel.Width;

			myControls = new Control[keywords.Length];
			this.BackgroundImage = ANGF.Properties.Resources.titilevisbase001;
		}

		private void PreTitle_Click(object sender, EventArgs e)
		{
			Completion();
		}
	}
}
